package ${domain}.frame.utils;

import java.util.Arrays;

public class StringUtil {

    /**
     * 字符串右填充
     *
     * @param text   待填充字符串
     * @param length 总长度
     * @param c      填充字符
     * @return 补充完成的字符串
     */
    public static String padRight(String text, int length, char c) {
        if (text == null) {
            throw new IllegalArgumentException("text can not be null!");
        }
        if (text.length() > length) {
            throw new IllegalArgumentException("text's length " + text.length() + " is too long!");
        }
        char[] array = new char[length];
        Arrays.fill(array, text.length(), length, c);
        System.arraycopy(text.toCharArray(), 0, array, 0, text.length());
        return new String(array);
    }

    /**
     * 字符串左填充
     *
     * @param text   待填充字符串
     * @param length 总长度
     * @param c      填充字符
     * @return 补充完成的字符串
     */
    public static String padLeft(String text, int length, char c) {
        if (text == null) {
            throw new IllegalArgumentException("text can not be null!");
        }
        if (text.length() > length) {
            throw new IllegalArgumentException("text's length " + text.length() + " is too long!");
        }
        char[] array = new char[length];
        Arrays.fill(array, 0, length - text.length(), c);
        System.arraycopy(text.toCharArray(), 0, array, length - text.length(), text.length());
        return new String(array);
    }

    public static int getByteLength(String str) {
        int length = str.replaceAll("[^\\x00-\\xff]", "**").length();
        return length;
    }

    public static String upperFirstWord(String str) {
        String temp = str.substring(0, 1);
        return temp.toUpperCase() + str.substring(1);
    }

    public static boolean isEmpty(String value) {
        int strLen;
        if (value == null || (strLen = value.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(value.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }


    public static boolean isNotEmpty(String value) {
        return !isEmpty(value);
    }


    /**
     * 检查对象是否为数字型字符串,包含负数开头的。
     */
    public static boolean isNumeric(Object obj) {
        if (obj == null) {
            return false;
        }
        char[] chars = obj.toString().toCharArray();
        int length = chars.length;
        if (length < 1)
            return false;

        int i = 0;
        if (length > 1 && chars[0] == '-')
            i = 1;

        for (; i < length; i++) {
            if (!Character.isDigit(chars[i])) {
                return false;
            }
        }
        return true;
    }

    /**
     * 把通用字符编码的字符串转化为汉字编码。
     */
    public static String unicodeToChinese(String unicode) {
        StringBuilder out = new StringBuilder();
        if (!isEmpty(unicode)) {
            for (int i = 0; i < unicode.length(); i++) {
                out.append(unicode.charAt(i));
            }
        }
        return out.toString();
    }


    public static String toUnderlineStyle(String name) {
        StringBuilder newName = new StringBuilder();
        for (int i = 0; i < name.length(); i++) {
            char c = name.charAt(i);
            if (Character.isUpperCase(c)) {
                if (i > 0) {
                    newName.append("_");
                }
                newName.append(Character.toLowerCase(c));
            } else {
                newName.append(c);
            }
        }
        return newName.toString();
    }
}
